﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PM.Entity;
using PM.Services;
using PM.Common;
using System.Configuration;

namespace PM.Site.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void btnLogin_Click(object sender, EventArgs e)
        {

            if (txbUsername.Text == "" || txbPassword.Text == "")
            {
                lblLoginFailMsg.Text = "Enter Username and Password";
                //MessageBox.Show("Please Enter Username and Password", "Enter Login Details", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            UserAccount user;
            try
            {
                user = UserAccountServices.GetUserAccountByUserName(txbUsername.Text);
            }
            catch (System.Exception ex)
            {
                lblLoginFailMsg.Text = "Site temporally out of service";
                return;
            }

            if (user == null)
            {

                lblLoginFailMsg.Text = "Invalid Username or Password";
                //MessageBox.Show("Invalid Username or Password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                String sHashedPass = MD5Calculator.CalculateMD5Hash(txbPassword.Text);

                if (sHashedPass == user.Password)
                {
                    //Program.currentUser = User;
                    if (user.UserTypeId != 1)
                    {
                        return;
                    }
                    Session["UserName"] = user.UserName;
                    Session["UserTypeId"] = user.UserTypeId;
                    Session["ClientId"] = user.ClientId;

                    if (Request.QueryString["RequestUrl"] != null)
                    {
                        Response.Redirect(Request.QueryString["RequestUrl"].ToString());
                    }
                    else
                    {
                        //Response.Redirect("ClientsLoanAccount.aspx");
                        Response.Redirect("Default.aspx");
                    }

                }
                else
                {
                    lblLoginFailMsg.Text = "Invalid Username or Password";
                    //MessageBox.Show("Invalid Username or Password", "Invalid Login", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }



        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //Username.Text = "user";
            //Password.Text = "abc123";
            if (ConfigurationSettings.AppSettings["HelpDesk"] != null)
            {
                lblHelpDesk.Text = "Help Desk - " + ConfigurationSettings.AppSettings["HelpDesk"].ToString();
            }
            
        }
    }
}
